package com.yoomiito.app.ui.account;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.rxbus.RxBus;
import com.jakewharton.rxbinding3.view.RxView;
import com.jakewharton.rxbinding3.widget.RxTextView;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.Constant;
import com.yoomiito.app.MainActivity;
import com.yoomiito.app.R;
import com.yoomiito.app.model.bean.GuiMiResult;
import com.yoomiito.app.model.bean.UserInfo;
import com.yoomiito.app.present.account.InputCodeCheckPresent;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.EditTextUtil;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.utils.LoginUtils;
import com.yoomiito.app.utils.RxBusManager;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.utils.YMToast;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.router.Router;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * 输入并确认验证码
 * token以验证验证码获取到的token为准
 * 有瑰秘汇登录  优米登录两种情况
 */
public class InputCodeCheckActivity extends BaseActivity<InputCodeCheckPresent> {

    @BindView(R.id.tv_check_phone)
    TextView tvCheckPhone;
    @BindView(R.id.et_check_code)
    EditText etCheckCode;
    @BindView(R.id.code_time)
    TextView codeTime;
    @BindView(R.id.login)
    Button login;
    private String mPhone;
    private EditTextUtil mEditTextUtil;
    private int type;
    private String mGuiMiHuiId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_send_check_code;
    }

    @Override
    public InputCodeCheckPresent newP() {
        return new InputCodeCheckPresent(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mPhone = getIntent().getStringExtra("phone");
        type = getIntent().getIntExtra("type",-1);
        mGuiMiHuiId = getIntent().getStringExtra(Constant.DATA);

        tvCheckPhone.setText(getString(R.string.yi_send_phone) + mPhone);
        getP().startCountdownTask();
        mEditTextUtil = new EditTextUtil(etCheckCode, login);
        mEditTextUtil.setNumberLength(4);
        mEditTextUtil.setEditTextListener();
//        RxTextView.textChanges(etCheckCode)
//                .skip(1)
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(charSequence ->{
//                });
    }

    public void CountdownTimeTask(int time) {
        codeTime.setText(time + getString(R.string.send_code_time_hint));
        codeTime.setEnabled(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mEditTextUtil.setEditTextListener();
    }

    public void timeEndTask() {
        codeTime.setText("重新发送");
        codeTime.setEnabled(true);
    }

    public void success() {
        mEditTextUtil.deleteTextWatcher();
        Router.newIntent(context)
                .to(InvitationCodeActivity.class)
                .launch();
    }



    @OnClick({R.id.login,R.id.code_time})
    public void onViewClicked(View v) {
        switch (v.getId()){
            case R.id.code_time:
                getP().startCountdownTask();
                getP().requestSendCode(mPhone);
                break;
            case R.id.login:
                String openId = "";
                switch (type){
                    case Constant.WX_Login:
                        openId = SharedPref.getInstance(App.getInstance()).getString(Constant.WECHAT_OPEN_ID, "");
                        break;
                    case AllCode.GuiMiHui:
                        String phoneCode = etCheckCode.getText().toString().trim();
                        if (TextUtils.isEmpty(phoneCode)) {
                            YMToast.showText(getString(R.string.input_check_code_hint));
                            return;
                        }
                        getP().sureGuiMiHuiId(mGuiMiHuiId,mPhone,phoneCode);
                        return;
                }
                wxLoginCheckCode(openId);
                break;
        }
    }

    @Override
    public void eventMessage(EventMessage eventMessage) {
        super.eventMessage(eventMessage);
        if (AllCode.LoginSuccess.equals(eventMessage.getEventType())){
            this.finish();
        }
    }

    public void toMain(UserInfo userInfo){
        LoginUtils.INSTANCE.loginHandle(this,userInfo);
//        UserInfoConfig.loginPutUserConfig(userInfo,true);
//        EventMessage em = new EventMessage();
//        em.setEventType(AllCode.LoginSuccess);
//        EventBus.getDefault().post(em);
    }

    public void guiMIHuiLogin(GuiMiResult result){
        toMain(result.getUser());
    }

    private void wxLoginCheckCode(String openId){

        String phoneCode = etCheckCode.getText().toString().trim();
        if (TextUtils.isEmpty(phoneCode)) {
            getvDelegate().toastShort(getString(R.string.input_check_code_hint));
            return;
        }
        getP().requestVerifyCode(phoneCode, mPhone, openId);
    }

    @Override
    protected void onDestroy() {
        getP().destroyCountdownTask();
        mEditTextUtil.deleteTextWatcher();
//        RxBusManager.unregisterRxBusManager(this);
        super.onDestroy();
    }
}
